#include "CGameManager.h"

#include "CState.h"
#include "CEventReceiver.h"
#include "CNetworkManager.h"
#include "CChatBox.h"
#include "CImageGUISkin.h"
#include "CMinion.h"

#include <ctime>

CGameManager::CGameManager()
{
  //! Load Configuration
  stringc ip;
  u16 port;
  u16 gamePort;
  int fullscreen, x, y, color;

  IXMLReader *xml = (IXMLReader*) createIrrXMLReaderUTF16("config.xml");
  // file could not be opened - Default stats
  if (xml == 0)
  {
    std::cout << "Error: Could not load config.xml" << std::endl;
    return;
  }

  // parse the file until end reached
  while(xml->read())
  {
    // Check for minion node
    stringw node(xml->getNodeName());
    if ( node == L"server" )
    {
      ip = xml->getAttributeValue(L"ip");
      port = xml->getAttributeValueAsInt(L"port");
    }

    else if ( node == L"game" )
    {
      gamePort = xml->getAttributeValueAsInt(L"port");
    }

    else if ( node == L"device" )
    {
      fullscreen = xml->getAttributeValueAsInt(L"fullscreen");
      x = xml->getAttributeValueAsInt(L"x");
      y = xml->getAttributeValueAsInt(L"y");
      color = xml->getAttributeValueAsInt(L"color");
    }
  }

  // Create the Irrlicht device
  device = createDevice(
    DRIVER_TYPE,
    dimension2d<u32>(x, y),
    color,
    fullscreen
    //true
    );

	// Set up the Engine pointer
	device->setWindowCaption(GAME_TITLE);

	driver = device->getVideoDriver();
	smgr = device->getSceneManager();
	env = device->getGUIEnvironment();

	for (u32 i=0; i<EGDC_COUNT ; ++i)
	{
    SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
    col.setAlpha(255);
    env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
  }

  // Random seed
  srand( time(NULL) );

  //! Skin settings


  IGUISkin* skin = new CImageGUISkin(env , "default.skin");
	IGUIFont* font = env->getFont("guifont.xml");
	if (font)
		//skin->setFont(font);
		skin->setFont(font, EGDF_BUTTON);


	skin->setFont(env->getBuiltInFont(), EGDF_TOOLTIP);

	skin->setColor(EGDC_BUTTON_TEXT, SColor(255, 255, 255, 255));
	env->setSkin(skin);

	skin->setSize(EGDS_BUTTON_WIDTH, 200);
	skin->setSize(EGDS_BUTTON_HEIGHT, 25);


	// Event receiver
  receiver = new CEventReceiver(this);
  device->setEventReceiver(receiver);

  // Network Manager
  net = new CNetworkManager(this, ip, port);
  net->setGamePort(gamePort);

  device->getCursorControl()->setVisible(false);

  // Global Chatbox
  chat = new CChatBox(this);

  // Minion Database
  CMinion::loadMinionBase(driver);
}

bool CGameManager::run()
{
  while ( device->run() )
  {
    if ( !stateStack.empty() )
    {
      (*stateStack.getLast())->run();


      // aifa more cute features haha ( An Axe following cursor )
      // pwnt by poz now
       position2d<s32> m = device->getCursorControl()->getPosition();

       driver->draw2DImage(driver->getTexture("Axe.png"),
        m,
        rect<s32>(0,0,36,44),
        0,
        SColor(255, 255, 255, 255),
        true);
      //
      driver->endScene();
      net->readPacket();
    }
    else
      break;
	}
	device->drop();

  return false;
}

void CGameManager::pushState(CState *state)
{
  if ( !stateStack.empty() )
    (*stateStack.getLast())->disable();

  stateStack.push_back(state);
  state->init();
}

void CGameManager::popState()
{
  list <CState*>::Iterator it = stateStack.getLast();

  (*it)->close();
  stateStack.erase(it);


  if ( !stateStack.empty() )
    (*stateStack.getLast())->enable();
}

void CGameManager::setPlayerID(u32 playerID)
{
  this->playerID = playerID;
}

void CGameManager::setPlayer(COverlord *player)
{
  this->player = player;
}
